<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="65%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="客户名称" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            placeholder="请输入客户名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="contactName">
          <el-input
            v-model="queryParams.contactName"
            placeholder="请输入联系人"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactNumber">
          <el-input
            v-model="queryParams.contactNumber"
            placeholder="请输入联系电话"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="客户名称" align="center" prop="customerName" />
        <el-table-column label="企业类型" align="center" prop="enterpriseType">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.FOLLOW_UP_CUSTOMER_TYPE" :value="scope.row.enterpriseType" />
          </template>
        </el-table-column>
        <el-table-column label="业务来源" align="center" prop="bussinessResource">
          <template #default="scope">
            <dict-tag
              :type="DICT_TYPE.BIZ_EXPAND_BUSINESS_TYPE"
              :value="scope.row.bussinessResource"
            />
          </template>
        </el-table-column>
        <el-table-column label="联系人" align="center" prop="contactName" />
        <el-table-column label="联系电话" align="center" prop="contactNumber" />
        <!-- <el-table-column label="所在地区" align="center" prop="location" /> -->
        <el-table-column label="详细地址" align="center" prop="address" />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <template #footer>
      <el-button @click="submit" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ExpandTargetCustomerApi, ExpandTargetCustomer } from '@/api/biz/expand/targets/customer'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

const emit = defineEmits(['success'])

/** 拓展目标_客户 列表 */
defineOptions({ name: 'ExpandTargetCustomer' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ExpandTargetCustomer[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  customerName: undefined,
  enterpriseType: undefined,
  bussinessResource: undefined,
  contactName: undefined,
  contactNumber: undefined,
  location: undefined,
  address: undefined,
  createTime: [],
  ownerUserId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
/** 打开弹窗 */
const open = async (id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = '拓展客户'
  handleQuery()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ExpandTargetCustomerApi.getExpandTargetCustomerPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const customerData = ref({})
const handleRowCheckboxChange = (records: ExpandTargetCustomer[]) => {
  customerData.value = records[0]
}

const submit = () => {
  dialogVisible.value = false
  emit('success', customerData.value)
}
</script>
